/**
 * 
 */
package dms.presentation.action;

import java.io.PrintWriter;
import java.nio.charset.Charset;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import dms.model.Building;
import dms.model.Campus;
import dms.model.util.PageIterator;
import dms.service.BuildingService;
import dms.service.CampusService;
import dms.util.ContextUtil;

/**
 * @author lrz
 *
 */
public class AjaxFindBuilding extends Action {
	
	private static Log log = LogFactory.getLog(AjaxFindBuilding.class);
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		long campusId;
		try {
			campusId = Long.valueOf(request.getParameter("id"));
		} catch (Exception e) {
			log.error("解析校区ID错误");
			return null;
		}
		response.setContentType("text/html;charset=UTF-8");
		BuildingService buildingService = (BuildingService) ContextUtil.getContext().getBean("buildingService");
		CampusService campusService = (CampusService) ContextUtil.getContext().getBean("campusService");
		Campus campus = campusService.findById(campusId);
		PageIterator<Building> iter = buildingService.findByCampus(0, 0, campus);
		
		StringBuilder responseXML = new StringBuilder("<response>");
		while (iter.hasNext()) {
			Building b = iter.next();
			
			responseXML.append("<building>");
			responseXML.append("<id>").append(b.getId()).append("</id>");
			responseXML.append("<name>").append(b.getName()).append("</name>");
			responseXML.append("</building>");
		}
		responseXML.append("</response>");	
	
		
		PrintWriter writer = response.getWriter();
		
		writer.print(responseXML.toString());
		
		return null;
		
	}
}
